# Author: HZT
# Date: 2023/9/8
import re
from datetime import datetime

import pyautogui
from selenium.webdriver.common.by import By

from config.config_loader import config
from page.web_sk_jk import WebSkJk
from page.web_ck_jy import WebCkJy
from page.web_ck_top import WebCkTop
from page.web_sk_top import WebSkTop
from utils import UtilsDriver, generate_random_string, login_with_session, GetLog, UtilsDriver2
from page.web_ck_dd import WebCkDd
import time
from selenium import webdriver


class TestWebCkJy:
    def setup_class(self):
        self.log = GetLog.get_log()
        self.driver = UtilsDriver.get_web_driver()
        self.driver.get(config.URL)
        login_with_session('system2', self.driver)
        self.driver2 = UtilsDriver2.get_web_driver()
        self.driver2.get(config.URL)
        login_with_session('system1', self.driver2)
        # 实例化
        self.ckJy = WebCkJy(self.driver2)
        self.cKTop = WebCkTop(self.driver2)
        self.skJk = WebSkJk(self.driver)
        self.ckDd = WebCkDd(self.driver2)
        self.skTop = WebSkTop(self.driver)

    def teardown_class(self):
        # 在所有测试用例执行完毕后，关闭浏览器
        # UtilsDriver.get_web_driver().quit()
        pass

    # 卖卡之后交易记录页面校验无误
    def test01_ck_jk(self):
        pyautogui.hotkey('alt', 'tab')
        time.sleep(2)
        self.skJk.sk_jk_click_jk()
        # 定位开关元素
        switch = self.skJk.sk_jk_get_ksjk()
        # 判断开关状态
        if switch.get_attribute("aria-hidden") == "true":
            # 开关是关闭状态，点击开关
            self.skJk.sk_jk_click_ksjk()
        pyautogui.hotkey('alt', 'tab')
        time.sleep(2)
        km = generate_random_string(15)
        count = 1  # 发卡数
        me = '100'  # 发卡金额
        filename = "steam_card.png"  # 卡图名称
        self.ckDd.ck_dd_click_jdd()
        self.ckDd.ck_dd_ck(self.driver2, me, filename, count, kz="t-Steam", km=km)
        try:
            # 获取余额
            ye = self.ckTop.ck_top_get_ye()
            self.ckJk.ck_jk_scyet(self.driver)
            self.ckJk.ck_jk_click_clcg()
            id = self.ckJk.ck_jk_get_ids()[0].text
            time.sleep(1)
            self.ckJk.ck_jk_click_cgtj()
            self.ckJy.ck_jy_click_jyjl()

            # 判断交易id是否降序排列
            elements = self.ckJy.ck_jy_get_jyid()
            data_keys = []
            for element in elements:
                data_keys.append(int(element.get_attribute("data-key")))
            sorted_data_keys = sorted(data_keys, reverse=True)
            # 核对卡片ID，面额，汇率，卡种，交易类型
            # 获取详情
            xq = self.ckJy.ck_jy_get_xq()
            # 提取面额
            me = re.search(r"面额：([\d.]+)", xq).group(1)
            # 提取卡密
            km2 = xq.split("代码：")[1]
            assert data_keys == sorted_data_keys
            assert id == self.ckJy.ck_jy_get_kpid()
            assert "购卡" == self.ckJy.ck_jy_get_jylx()
            assert "100.00" == me
            assert km == km2
        except Exception as e:
            self.log.error("接卡页未接到卡片,原因{}".format(e))
            # 截图
            self.ckJk.base_get_img()
            # 抛异常
            raise

    def test02_ck_jk(self):
        try:
            self.ckTop.ck_top_click_zz()
            self.ckTop.ck_top_zz("CK64E41DFB2CC20", "500", "转账！")
            self.ckTop.ck_top_click_zzqd()
            self.ckJk.ck_jk_click_ckzx()
            time.sleep(0.5)
            self.ckJy.ck_jy_click_jyjl()
            # 核对转账金额，交易类型，收款公司
            jyxx = self.ckJy.ck_jy_get_jyxx()
            company = re.search(r"收款公司：(.+)", jyxx).group(1)
            assert "500.00" == self.ckJy.ck_jy_get_je()
            assert "转出" == self.ckJy.ck_jy_get_jylx()
            assert "子拓出卡公司" == company
        except Exception as e:
            self.log.error("错误原因{}".format(e))
            # 截图
            self.ckJk.base_get_img()
            # 抛异常
            raise
